/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.sac.bc;

import edu.upn.sigemfi.rec.bc.CreditoFacadeLocal;
import edu.upn.sigemfi.rec.bc.CuotaCreditoFacadeLocal;
import edu.upn.sigemfi.rec.be.Credito;
import edu.upn.sigemfi.sac.be.ProductoSbs;
import edu.upn.sigemfi.sac.be.SubProducto;
import edu.upn.sigemfi.seg.bc.UsuarioFacadeLocal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author GABRIEL
 */
public class CreditoDelegate {

    private CreditoFacadeLocal local;

    public CreditoDelegate() {
        local = lookupCreditoFacade();
    }

    public List<Credito> buscarCreditosVencidos() throws Exception{
        List<Credito> creditos = null;

        try {
            creditos = local.findAllVencidos();
        } catch (Exception ex) {
            local = lookupCreditoFacade();
            throw new Exception("Buscar Creditos Vencidos... " + ex.getMessage());
        }

        return creditos;
    }

    public List<Credito> buscarCreditosVigentes() throws Exception{
        List<Credito> creditos = null;

        try {
            creditos = local.findAllVigentes();
        } catch (Exception ex) {
            local = lookupCreditoFacade();
            throw new Exception("Buscar Creditos Vigentes... " + ex.getMessage());
        }

        return creditos;
    }

    public List<Credito> todos() throws Exception{
        List<Credito> creditos = null;

        try {
            creditos = local.findAll();
        } catch (Exception ex) {
            local = lookupCreditoFacade();
            throw new Exception("Listar Creditos... " + ex.getMessage());
        }

        return creditos;
    }

    private CreditoFacadeLocal lookupCreditoFacade() {
        try {
            Context c = new InitialContext();
            return (CreditoFacadeLocal) c.lookup("java:comp/env/CreditoFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private CuotaCreditoFacadeLocal lookupCuotaCreditoFacade() {
        try {
            Context c = new InitialContext();
            return (CuotaCreditoFacadeLocal) c.lookup("java:comp/env/CuotaCreditoFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }



}
